package com.itheima.stock.service.impl;

import com.itheima.stock.mapper.SysPermissionMapper;
import com.itheima.stock.pojo.SysPermission;
import com.itheima.stock.service.PreService;
import com.itheima.stock.utils.IdWorker;
import com.itheima.stock.vo.req.PerReq;
import com.itheima.stock.vo.resp.PerEveName;
import com.itheima.stock.vo.resp.R;
import com.itheima.stock.vo.resp.TianJiaRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PerServiceImpl implements PreService {


    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    @Autowired
    private IdWorker idWorker;

    //权限查询
    @Override
    public R<List<SysPermission>> findAll() {
        List<SysPermission> all = sysPermissionMapper.findAll();
        return R.ok(all);
    }

    //权限添加
    @Override
    public R<TianJiaRes> addPer(PerReq perReq) {
        perReq.setPid(idWorker.nextId());
        perReq.setId(idWorker.nextId());
        sysPermissionMapper.addPer(perReq);
        TianJiaRes tianJiaRes = new TianJiaRes();
        tianJiaRes.setCode("1");
        tianJiaRes.setMsg("操作成功");
        return R.ok(tianJiaRes);
    }

    //权限修改
    @Override
    public R<TianJiaRes> updatePer(PerReq perReq) {
        sysPermissionMapper.updatePer(perReq);
        TianJiaRes tianJiaRes = new TianJiaRes();
        tianJiaRes.setCode("1");
        tianJiaRes.setMsg("操作成功");
        return R.ok(tianJiaRes);
    }

    //权限删除
    @Override
    public R<TianJiaRes> upDeletePer(String id) {
        sysPermissionMapper.upDeletePer(id);
        TianJiaRes t = TianJiaRes.builder().code("1").msg("操作成功").build();
        return R.ok(t);
    }

    @Override
    public R<List<PerEveName>> findPerEveName() {
        List<PerEveName> perEveName = sysPermissionMapper.findPerEveName();
        PerEveName DJ = PerEveName.builder().id("0").title("顶级菜单").level("0").build();
        perEveName.add(0, DJ);
        return R.ok(perEveName);
    }
}
